package com.socio.resources;

import java.util.Arrays;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.google.gson.Gson;
import com.socio.behaviors.DAOBehavior;
import com.socio.behaviors.UserUDIDBehavior;
import com.socio.model.ClientUser;
import com.socio.model.responses.UserUpdateResponse;
import com.socio.utils.FieldAccessor.FieldAccessorField;

@Path("/user")
public class UserResource {

	private static final Gson gson = new Gson();
	
	@Path("/update")
    @POST
    @Produces(MediaType.TEXT_PLAIN)
    @Consumes(MediaType.APPLICATION_JSON)
	public String updateUser(String json) {
		
		// setup
		UserUpdateResponse response = new UserUpdateResponse(false);
        ClientUser updatedUser = gson.fromJson(json, ClientUser.class);
		DAOBehavior userDAO = new UserUDIDBehavior();
		
		// update user with nickname
        Object result = userDAO.update(updatedUser.getAndroidUDID(), Arrays.asList(FieldAccessorField.UserFacebookID),
        		Arrays.asList((Object)updatedUser.getFacebookID()), null);
        if(result != null)
        	response.setSuccess(true);
        
        return gson.toJson(response);
	}
}
